//
//  XHRWebViewController.m
//  BuDeJie
//
//  Created by haoran on 16/4/7.
//  Copyright © 2016年 xuhaoran. All rights reserved.
//

#import "XHRWebViewController.h"
#import <WebKit/WebKit.h>
@interface XHRWebViewController ()
/** <#名称#> **/
@property (nonatomic, weak) WKWebView *webView;

@property (weak, nonatomic) IBOutlet UIProgressView *progressView;
@end

@implementation XHRWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    //创建WKWebView
    [self setupWebView];
}

#pragma mark ---- 创建WKWebView
-(void)setupWebView{
    //创建webView
    WKWebView * webView = [[WKWebView alloc]initWithFrame:self.view.bounds];
    self.webView = webView;
    
    //考虑progressView ,把webView 插入到最底层
    [self.view insertSubview:webView atIndex:0];
    
    //加载网页
    NSURLRequest * request = [NSURLRequest requestWithURL:_url];
    [webView loadRequest:request];
    
    //KVO 让self监听webview对象的estimatedProgress属性
    [webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
    
}

//只要监听到有属性有新的值就会调用
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{

    self.progressView.progress = self.webView.estimatedProgress;
    
    //如果加载完毕(100%),隐藏progressView
    self.progressView.hidden =  self.progressView.progress >= 1 ;

}

//KVO一定要移除观察者
-(void)dealloc{
    //移除jiant
    [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
}


@end
